/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MaterialPack;

import Supporter.ImageSupporter;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Graphics;
import javax.xml.parsers.FactoryConfigurationError;

/**
 *
 * @author Van persie
 */
public class ContextDialog extends MaterialObject {
    
    public static final int VERTICAL_TOP = 0;
    public static final int VERTICAL_MIDDLE = 1;
    public static final int VERTICAL_BOTTOM = 2;
    public static final int HORIZON_LEFT = 0;
    public static final int HORIZON_MIDDLE = 1;
    public static final int HORIZON_RIGHT = 2;
            
    boolean ShowImage = false; 
    
    String[] text = null;
   // Image[] icons = null;
    private boolean transBack = false;
    int orgX,orgY,tempWid,tempHei;
    int Incrment = 5;
    int Up = 0;
    Image orgBackground;
    boolean isChanged = false;
    private int horizon =0;
    private int vertical=0;

    int ShowTime ;  //  remain time and diff Pet Info vs Item Info
    
    public ContextDialog(String Backgroundpath)
    {
        //super.GetGraphicsInfo(x, y, wid, hei);
        //icons = new Image[1];
        //super.LoadImage(Backgroundpath,false, 0, 0);
        orgBackground = new ImageSupporter().getImage(Backgroundpath);
        orgBackground =  ImageSupporter.transparent(ImageSupporter.alphaBlend(orgBackground));
        
        //background = ImageSupporter.alphaBlend(background);
        isVisible = false;
    }
    public ContextDialog(Image img)
    {
        //super.GetGraphicsInfo(x, y, wid, hei);
        //icons = new Image[1];
        //super.LoadImage(Backgroundpath,false, 0, 0);
        //background = Image.createImage(img);
        background = orgBackground =  img;
        
        
        //background = ImageSupporter.alphaBlend(background);
        isVisible = false;
    }
   
    
    public void SetAnchor(int hor, int ver)
    {
        horizon = hor;
        vertical = ver;
    }
    public void Show(String[] txt, int x, int y , int wid, int hei, int showtime, int vecUp)
    {        
        ShowImage = false;
        Up = vecUp;
        ShowTime = showtime;
        text = txt;
        super.GetGraphicsInfo(x, y, wid, hei);
        //orgBackground = ImageSupporter.scaleImage(background, wid, hei);
        
        
        tempWid = wid/6;
        tempHei = hei/6;
        orgX = x ;
        orgY = y ;
        xPos = orgX + width - tempWid;
        yPos = orgY + height - tempHei;
        background = ImageSupporter.scaleImage(orgBackground, tempWid, tempHei);
        isVisible = true;
        isChanged = false;
        
    }
//    public void Show(Image icon, int x, int y , int wid, int hei, int showtime, int vecUp)
//    {
//        ShowImage = true;
//        Up = vecUp;
//        ShowTime = showtime;
//        icons[0] = ImageSupporter.transparent(ImageSupporter.scaleImage(icon, 2*width/3,2*height/3));
//        
//        super.GetGraphicsInfo(x, y, wid, hei);
//        orgBackground = ImageSupporter.scaleImage(background, wid, hei);
//        
//        
//        tempWid = wid/6;
//        tempHei = hei/6;
//        orgX = x ;
//        orgY = y ;
//        xPos = orgX + width - tempWid;
//        yPos = orgY + height - tempHei;
//        background = ImageSupporter.scaleImage(orgBackground, tempWid, tempHei);
//        isVisible = true;
//        isChanged = false;
//    }
    public void Show(String txt, int x, int y , int wid, int hei, int showtime, int VecUp)
    {       
        ShowImage = false;
        Up = VecUp;
        ShowTime = showtime;
        text = new String[1];
        text[0] = txt;
        super.GetGraphicsInfo(x, y, wid, hei);
        //orgBackground = ImageSupporter.scaleImage(background, wid, hei);
        
        
        tempWid = wid/6;
        tempHei = hei/6;
        orgX = x ;
        orgY = y ;
        xPos = orgX + width - tempWid;
        yPos = orgY + height - tempHei;
        background = ImageSupporter.scaleImage(orgBackground, tempWid, tempHei);
        isVisible = true;
        isChanged = false;
        
    }
    public void Hide()
    {
        isVisible = false;
    }
    public boolean  Expand()
    {
        
        tempWid += Incrment;
        tempHei += (Incrment*2/3);
        
        xPos = orgX;
        if(getHorizon() == HORIZON_LEFT)        
            xPos += (width - tempWid);
        else if(getHorizon() == HORIZON_MIDDLE)
            xPos += ((width - tempWid))/2;
        
        yPos = orgY;
        if(getVertical() == VERTICAL_TOP)
            yPos += (height - tempHei);
        else if(getVertical() == VERTICAL_MIDDLE)
            yPos += (height - tempHei)/2;
        
        if( tempWid >= getWidth() || tempHei >= getHeight())
        {
            
            setxPos(orgX);
            setyPos(orgY);
            background = ImageSupporter.scaleImage(orgBackground, width, height);
            isChanged = true;
            return true;
        }
        background = ImageSupporter.scaleImage(orgBackground, tempWid, tempHei);
        return false;
    }
    public boolean MyUpdate()
    {
        if(isVisible == false)
            return false;
            
        if( isChanged == false)
        {
            Expand();
        }
        else 
            yPos -= Up; 
        
        if(ShowTime == 0)
               isVisible = false;
           else 
               ShowTime --;    
        return !isVisible;
    }
    public void paint(Graphics g)
    {
        if(isVisible)
        {
            //int col = g.getColor();
            g.drawImage(background, xPos, yPos, Graphics.TOP|Graphics.LEFT);
            if(isChanged)
            {
                g.setFont(Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_ITALIC, Font.SIZE_SMALL));
                
                //g.setColor(200, 150, 250);
                if(!ShowImage)
                    if(text.length > 1)
                        for(int i = 0; i < text.length ; i++)
                            g.drawString(text[i], xPos  + width/6 , yPos + 3 + i*(height/4) ,Graphics.TOP | Graphics.LEFT );
                else
                        g.drawString(text[0], xPos  + width/8 , yPos + (height/4) ,Graphics.TOP | Graphics.LEFT );
//                    g.drawImage(icons[0], xPos + width/2, yPos + 15, Graphics.HCENTER | Graphics.TOP);
            }
            //g.setColor(col);
        }
    }
    
      /**
     * @return the transBack
     */
    public boolean isTransBack() {
        return transBack;
    }

    /**
     * @param transBack the transBack to set
     */
    public void setTransBack(boolean bit) {
        transBack = bit;
    }

    /**
     * @return the vertical
     */
    public int getVertical() {
        return vertical;
    }

    /**
     * @param vertical the vertical to set
     */
    public void setVertical(int vertical) {
        this.vertical = vertical;
    }

    /**
     * @return the horizon
     */
    public int getHorizon() {
        return horizon;
    }

    /**
     * @param horizon the horizon to set
     */
    public void setHorizon(int horizon) {
        this.horizon = horizon;
    }

}
